
package com.example.myapplication_02.adapter;



import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.myapplication_02.R;
import com.example.myapplication_02.entity.Msg;

import java.util.List;

public class MsgAdapter extends ArrayAdapter<Msg> {
    private int resourceId;

    public MsgAdapter(@NonNull Context context, int resource, @NonNull List<Msg> objects) {
        super(context, resource, objects);
        resourceId = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Msg msg = getItem(position);
        View msgView = LayoutInflater.from(getContext()).inflate(resourceId,null);
        LinearLayout sendLayout = msgView.findViewById(R.id.layout_send);
        LinearLayout recLayout = msgView.findViewById(R.id.layout_rec);
        TextView sendText = msgView.findViewById(R.id.text_send);
        ImageView sendHead = msgView.findViewById(R.id.img_send);
        TextView recText = msgView.findViewById(R.id.text_rec);
        ImageView recdHead = msgView.findViewById(R.id.img_rec);

        if(msg.getMsgType() == 0)  {
            sendLayout.setVisibility(View.VISIBLE);
            recLayout.setVisibility(View.GONE);
            sendText.setText(msg.getContent());
            sendHead.setImageResource(msg.getHeadImg());

        }else {
            sendLayout.setVisibility(View.GONE);
            recLayout.setVisibility(View.VISIBLE);
            recText.setText(msg.getContent());
            recdHead.setImageResource(msg.getHeadImg());

        }
        return msgView;
    }
}